package cn.jolyoulu.modules.druidmodule.utils.datasource;


import cn.jolyoulu.common.druid.utils.datasource.DruidUtils;
import org.junit.Before;
import org.junit.Test;
import cn.jolyoulu.common.druid.constant.DBConstant;

import javax.sql.DataSource;
import java.sql.Connection;
import java.util.Objects;
import java.util.Properties;


/**
 * @Author LuZhouJin
 * @Date 2023/7/25
 */
class DruidUtilsTest {

    private static DataSource dataSource;

    @Before
    void creatDataSource() {
        System.out.println("建立连接池");
        //配置
        Properties properties = new Properties();
        properties.setProperty("driverClassName", "com.mysql.cj.jdbc.Driver");
        properties.setProperty("url", DBConstant.getMysqlUrl("localhost", "3306"));
        properties.setProperty("username", "root");
        properties.setProperty("password", "123456");
        dataSource = DruidUtils.creatDataSource("1", properties);
        assert Objects.nonNull(dataSource) : "创建连接池失败";
    }

    @Test
    void getDataSource() {
        DataSource dataSource = DruidUtils.getDataSource("1");
        assert Objects.nonNull(dataSource) : "获取数据源失败";
        System.out.println("DruidUtils.getDataSource() => " + dataSource);
    }

    @Test
    void cloDataSource() {
        DruidUtils.cloDataSource("1");
        System.out.println("关闭连接");
    }

    @Test
    void getConnection() {
        Connection connection = DruidUtils.getConnection("1");
        assert Objects.nonNull(connection) : "获取连接失败";
        System.out.println("DruidUtils.getConnection() => " + connection);
    }
}